package kompics.pp;

import java.util.Set;

import se.sics.kompics.Component;
import se.sics.kompics.ComponentDefinition;
import se.sics.kompics.Kompics;
import se.sics.kompics.address.Address;
import se.sics.kompics.launch.Topology;
import se.sics.kompics.network.Network;
import se.sics.kompics.network.mina.MinaNetwork;
import se.sics.kompics.network.mina.MinaNetworkInit;

/**
 * @author shadid
 * 
 */
public class MainContainer extends ComponentDefinition {

	private static int selfId;
	Topology topology = Topology.load(System.getProperty("topology"), selfId);

	public static void main(String[] args) {
		selfId = Integer.parseInt(args[0]);
		Kompics.createAndStart(MainContainer.class, 8);
	}

	public MainContainer() {

		Address self = topology.getSelfAddress();
		Set<Address> neighborSet = topology.getNeighbors(self);
		Address randomNeighbor = neighborSet.iterator().next(); // keep it ugly for now

		// Component creation
		Component sPeer = create(SimplePeer.class);
		Component network = create(MinaNetwork.class);

		connect(sPeer.getNegative(Network.class),
				network.getPositive(Network.class));

		trigger(new MinaNetworkInit(self), network.getControl());
		trigger(new PeerContInit(null, neighborSet, self), sPeer.getControl());

		trigger(new PingMessage(randomNeighbor, self), sPeer.getNegative(Network.class));

	}

}
